/*
 *  SyncIT , easy synchronization of files between computers and devices.
 *  Copyright (C) 2011  Christian Masus <cmasus(at)gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version. If redistributing and/or modifying
 *  this file, this notice must be included in the redistributed or
 *  modified copy.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gui.javagui;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;

@SuppressWarnings("serial")
public class SyncTablePortEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {

	private int clickCountToStart;
	private int min;
	private int max;
	private JTextField textField;

	public SyncTablePortEditor() {
		clickCountToStart = 2;
		min = 0;
		max = 65535;

		textField = new JTextField();
		textField.setHorizontalAlignment(JTextField.TRAILING); // Aligning while editing
		textField.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e) {
		if(isValidPortNumber()) {
			fireEditingStopped();
		}
		else {
			textField.setBorder(BorderFactory.createLineBorder(Color.RED));
			showMessageDialog();
		}
	}

	public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
		textField.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		textField.setText(Integer.toString((Integer)value));
		return textField;
	}

	public Object getCellEditorValue() {
		return textField.getText();
	}

	public boolean isCellEditable(EventObject e) {
		if(e instanceof MouseEvent) {
			return ((MouseEvent)e).getClickCount() >= clickCountToStart;
		}
		return true;
	}

	public boolean shouldSelectCell(EventObject anEvent) {
		return true;
	}

	public boolean stopCellEditing() {
		if(isValidPortNumber()) {
			fireEditingStopped();
			return true;
		}
		else {
			textField.setBorder(BorderFactory.createLineBorder(Color.RED));
			showMessageDialog();
			return false;
		}
	}

	public void cancelCellEditing() {
		fireEditingCanceled();
	}



	private boolean isValidPortNumber() {
		try {
			String value = textField.getText();
			int val = Integer.parseInt(value);
			if(val >= min && val <= max) {
				return true;
			}
		} catch(NumberFormatException e) {
		}
		return false;
	}

	private void showMessageDialog() {
		JOptionPane.showMessageDialog(JavaGui.getFrame(),
				"Port number must be between " + min + " and " + max + ".",
				"Invalid port number",
				JOptionPane.INFORMATION_MESSAGE);
	}

}
